package com.cryptape.cita_wallet;

import android.support.annotation.NonNull;
import android.util.Log;

import com.cryptape.cita_wallet.item.response.EthTransactionStatus;
import com.cryptape.cita_wallet.service.http.HttpService;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class RxTimerUtil {

    private static Disposable mDisposable;
/**
 * Created by dengming on 7/2/20.
 * milliseconds毫秒后执行next操作
 *
 * @param milliseconds
 * @param next
 */

    /**
     * milliseconds毫秒后执行next操作
     *
     * @param milliseconds
     * @param next
     */

    public static void timer(long milliseconds, final IRxNext next, int take,String txHash) {
        Observable.interval(milliseconds, TimeUnit.MILLISECONDS)
                .take(take)
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.newThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long result) {
                        EthTransactionStatus response = HttpService.getEthTransactionStatusNew(txHash);
                        if(response == null) {
                            Log.e("MainActivity", "response is null");
                        }else{
                            Log.e("MainActivity", "response message:"+response.message+ " status:"+response.status);
                        }
                        if (response != null) {
                            if (response.result.status.equals("1")) {
                                Log.e("MainActivity","查询交易记录成功:"+txHash);
                                next.doNext(true);
                                cancel();
                                return;
                            }
                        }
                        Log.e("MainActivity","查询交易记录失败:"+txHash);
                        if(result >= take-1){
                            next.doNext(true);
                            cancel();
                        }
//                        if (next != null) {
//                            next.doNext(result);
//                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //取消订阅
                        next.doNext(false);
                        cancel();
                    }

                    @Override
                    public void onComplete() {
                        //取消订阅
//                        next.doNext(false);
//                        cancel();
                    }
                });
    }


    /**
     * 取消订阅
     */
    public static void cancel() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    public interface IRxNext {
        void doNext(Boolean result);
    }
}
